package io.renren.modules.wyzsphone.commWithWCF;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class CommonEncryptUtil {
	public static String aesEncrypt(String str, String key) throws Exception {
		if (str == null || key == null)
			return null;
		SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), "AES");
		Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
		cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

		byte[] bytes = cipher.doFinal(str.getBytes());

		byte[] result2 = Base64.encodeBase64(bytes);

		return new String(result2);
	}

	public static String aesDecrypt(String str, String key) throws Exception {
		if (str == null || key == null)
			return null;
		Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
		cipher.init(Cipher.DECRYPT_MODE,
				new SecretKeySpec(key.getBytes("utf-8"), "AES"));

		byte[] bytes = Base64.decodeBase64(str.getBytes());
		bytes = cipher.doFinal(bytes);
		return new String(bytes, "utf-8");
	}
}